# code: utf8

# 基础方法
def split_project(project_origin):
    # 以-分割传入的参数project_origin
    project_tag_env = project_origin.split('-')

    # 发布环境默认pre
    deploy_env = 'pre'
    # 长度大于等于3则取alias_name tag env
    if len(project_tag_env) >= 3:
        alias_name, tag_or_branch, deploy_env = project_tag_env[:3]
    elif len(project_tag_env) == 2:
        alias_name, tag_or_branch, deploy_env = project_tag_env[:2]

    else:
        alias_name = project_tag_env[0]
        tag_or_branch = ''

    return alias_name, tag_or_branch, deploy_env


# 获取key:value
def get_kv_by_list(list, keyname, vname):
    """
    获取元素为dict的list的两个字段值，以k:v方式输出，如从：
     [
       { "aa": "aa1",
       "bb": "bb1"
       },
       { "aa": "aa2",
       "bb": "bb2"
       }
    ]

    输出 [{"aa1": "bb1","aa2": "bb2"}
    """
    res = {}
    for d in list:
        mydict = d.to_dict()
        if d:
            res[mydict.get(keyname)] = mydict.get(vname)
    return res
